﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mesuro.Graphing.Utilities;
using System.Reflection;

namespace Mesuro.Graphing.PropertiesControls {
	/// <summary>
	/// Interaction logic for SeriesProperties.xaml
	/// </summary>
	public partial class SeriesProperties : UserControl {

		#region DP: Axes
		public static readonly DependencyProperty AxesProperty = DependencyProperty.RegisterAttached( "Axes", typeof( Axes ), typeof( SeriesProperties ), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.None, AxesChangedStatic ) );
		public Axes Axes {
			get { return (Axes)GetValue( AxesProperty ); }
			set { SetValue( AxesProperty, value ); }
		}
		public static Axes GetAxes( DependencyObject element ) { return (Axes)element.GetValue( AxesProperty ); }
		public static void SetAxes( DependencyObject element, Axes value ) { element.SetValue( AxesProperty, value ); }
		private static void AxesChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) {
			//((SeriesProperties)sender).AxisSets = (e.NewValue is MultiAxes) ? ((MultiAxes)e.NewValue).AxisSetNames : new string[0];
		}
		#endregion

		#region DP: Plot
		public static readonly DependencyProperty PlotProperty = DependencyProperty.RegisterAttached( "Plot", typeof( Plot ), typeof( SeriesProperties ), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.None, PlotChangedStatic ) );
		public Plot Plot {
			get { return (Plot)GetValue( PlotProperty ); }
			set { SetValue( PlotProperty, value ); }
		}
		public static Plot GetPlot( DependencyObject element ) { return (Plot)element.GetValue( PlotProperty ); }
		public static void SetPlot( DependencyObject element, Plot value ) { element.SetValue( PlotProperty, value ); }
		private static void PlotChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) {
			SeriesProperties sp = sender as SeriesProperties;

			//if (e.OldValue != null)
			//    ((Plot)e.OldValue).RenderTypeChanged -= sp.OnPlotRenderTypeChanged;

			//Plot nPlot = e.NewValue as Plot;
			//if (nPlot != null)
			//    nPlot.RenderTypeChanged += sp.OnPlotRenderTypeChanged;

			//sp.OnPlotRenderTypeChanged( nPlot, new RoutedPropertyChangedEventArgs<string>( null, (nPlot == null) ? null : nPlot.RenderType ) );
		}

		#endregion

		#region DP: AxisSets
		public static readonly DependencyProperty AxisSetsProperty = DependencyProperty.RegisterAttached( "AxisSets", typeof( string[] ), typeof( SeriesProperties ), new FrameworkPropertyMetadata( new string[0], FrameworkPropertyMetadataOptions.None, AxisSetsChangedStatic ) );
		public string[] AxisSets {
			get { return (string[])GetValue( AxisSetsProperty ); }
			set { SetValue( AxisSetsProperty, value ); }
		}
		public static string[] GetAxisSets( DependencyObject element ) { return (string[])element.GetValue( AxisSetsProperty ); }
		public static void SetAxisSets( DependencyObject element, string[] value ) { element.SetValue( AxisSetsProperty, value ); }
		private static void AxisSetsChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) {
			SetMultipleAxisSets( sender, !(e.NewValue == null || ((string[])e.NewValue).Length < 2) );
		}
		#endregion

		#region DP: MultipleAxisSets
		public static readonly DependencyProperty MultipleAxisSetsProperty = DependencyProperty.RegisterAttached( "MultipleAxisSets", typeof( bool ), typeof( SeriesProperties ), new FrameworkPropertyMetadata( false, FrameworkPropertyMetadataOptions.None, MultipleAxisSetsChangedStatic ) );
		public bool MultipleAxisSets {
			get { return (bool)GetValue( MultipleAxisSetsProperty ); }
			set { SetValue( MultipleAxisSetsProperty, value ); }
		}
		public static bool GetMultipleAxisSets( DependencyObject element ) { return (bool)element.GetValue( MultipleAxisSetsProperty ); }
		public static void SetMultipleAxisSets( DependencyObject element, bool value ) { element.SetValue( MultipleAxisSetsProperty, value ); }
		private static void MultipleAxisSetsChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) { }
		#endregion

		#region DP: AdditionalProperties
		public static readonly DependencyProperty AdditionalPropertiesProperty = DependencyProperty.RegisterAttached( "AdditionalProperties", typeof( PlotProperties ), typeof( SeriesProperties ), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.None, AdditionalPropertiesChangedStatic ) );
		public PlotProperties AdditionalProperties {
			get { return (PlotProperties)GetValue( AdditionalPropertiesProperty ); }
			set { SetValue( AdditionalPropertiesProperty, value ); }
		}
		public static PlotProperties GetAdditionalProperties( DependencyObject element ) { return (PlotProperties)element.GetValue( AdditionalPropertiesProperty ); }
		public static void SetAdditionalProperties( DependencyObject element, PlotProperties value ) { element.SetValue( AdditionalPropertiesProperty, value ); }
		private static void AdditionalPropertiesChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) {
			PlotProperties props = e.NewValue as PlotProperties;
			SetHasAdditionalProperties( sender, props != null );
			SeriesProperties ser = sender as SeriesProperties;
			if (ser != null) {
				props.Plot = ser.Plot;
				//props.Axes = ser.Axes;
			}
		}
		#endregion

		#region DP: HasAdditionalProperties
		public static readonly DependencyProperty HasAdditionalPropertiesProperty = DependencyProperty.RegisterAttached( "HasAdditionalProperties", typeof( bool ), typeof( SeriesProperties ), new FrameworkPropertyMetadata( false, FrameworkPropertyMetadataOptions.None, HasAdditionalPropertiesChangedStatic ) );
		public bool HasAdditionalProperties {
			get { return (bool)GetValue( HasAdditionalPropertiesProperty ); }
			set { SetValue( HasAdditionalPropertiesProperty, value ); }
		}
		public static bool GetHasAdditionalProperties( DependencyObject element ) { return (bool)element.GetValue( HasAdditionalPropertiesProperty ); }
		public static void SetHasAdditionalProperties( DependencyObject element, bool value ) { element.SetValue( HasAdditionalPropertiesProperty, value ); }
		private static void HasAdditionalPropertiesChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) { }
		#endregion

		public SeriesProperties() {
			AxisSets = new string[0];
			InitializeComponent();
			cboRenderType.ItemsSource = GraphingRegistry.GetRendererNames();
		}
		private void OnPlotRenderTypeChanged( object sender, RoutedPropertyChangedEventArgs<String> e ) {
			Plot plot = sender as Plot;

			if (plot == null) {
				AdditionalProperties = null;
				return;
			}

			Type tProp = GraphingRegistry.GetAdditionalProperties( plot );
			ConstructorInfo cProp = (tProp == null) ? null : tProp.GetConstructor( Type.EmptyTypes );
			AdditionalProperties = (cProp == null) ? null : (PlotProperties)cProp.Invoke( null );
		}
		private void cmdEditSeries_Click( object sender, RoutedEventArgs e ) {

		}
	}
}
